package com.example.healkep.component.page;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.SwipeRefreshHeaderLayout;
import com.example.healkep.R;

public class RefreshHeader extends SwipeRefreshHeaderLayout {
    private TextView tvRefresh;

    private ProgressBar progressBar;

    private int mHeaderHeight;

    private boolean rotated = false;
    public RefreshHeader(Context context) {
        super(context);
    }

    public RefreshHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mHeaderHeight = getResources().getDimensionPixelOffset(R.dimen.refresh_header_height_60);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvRefresh = (TextView) findViewById(R.id.tvRefresh);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
    }

    @Override
    public void onRefresh() {
        progressBar.setVisibility(VISIBLE);
        tvRefresh.setText("正在翻页...");
    }

    @Override
    public void onPrepare() {
    }

    @Override
    public void onMove(int y, boolean isComplete, boolean automatic) {
        if (!isComplete) {
            progressBar.setVisibility(GONE);
            if (y > mHeaderHeight) {
                tvRefresh.setText("上一页");
                if (!rotated) {
                    rotated = true;
                }
            } else if (y < mHeaderHeight) {
                if (rotated) {
                    rotated = false;
                }
                tvRefresh.setText("上一页");
            }
        }
    }

    @Override
    public void onRelease() {
    }

    @Override
    public void onComplete() {
        rotated = false;
        progressBar.setVisibility(GONE);
        tvRefresh.setText("刷新完成");

    }

    @Override
    public void onReset() {
        rotated = false;
        progressBar.setVisibility(GONE);
        tvRefresh.setText("上一页");
    }
}
